#define  _CRT_SECURE_NO_WARNINGS 1

#include "test.h"

//函数的实现

//判断平局||棋盘是否满了
int is_Full(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')
			{
				return 0;//棋盘没满
			}
		}
	}
	return 1;//棋盘满了
}

//1.打印游戏初始界面
void menu()
{
	printf("******************************\n");
	printf("*********1. 开始游戏**********\n");
	printf("******************************\n");
	printf("*********0. 退出游戏**********\n");
	printf("******************************\n");
}

//2.初始化棋盘
void Initboard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			board[i][j] = ' ';
		}
	}
}

//3.打印棋盘的函数
void Putboard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			printf(" %c ", board[i][j]);
			if (j < col - 1)
			{
				printf("|");
			}
		}
		printf("\n");
		if (i < row - 1)
		{
			for (j = 0; j < col; j++)
			{
				printf("---");
if (j < col - 1)
{
	printf("|");
}
			}
		}
		printf("\n");
	}
}

//4.玩家下棋
void Playermove(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("玩家走\n");
	while (1)
	{
		printf("请输入坐标,输入格式例：2 3\n--->");
		scanf("%d %d", &i, &j);
		//判断坐标是否输入正确
		if (i >= 1 && i <= row && j >= 1 && j <= col)
		{
			//判断该坐标是否已填子
			if (board[i - 1][j - 1] == ' ')
			{
				board[i - 1][j - 1] = '*';
				break;
			}
			else
			{
				printf("输入错误，请重新输入\n");
			}
		}

	}
}

//电脑下棋
void PCmove(char board[ROW][COL], int row, int col)
{
	printf("电脑走\n");
	int flag = 1;
	int i = 0;
	int j = 0;
	//判断是否行中两子相连
	for (i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == '*' && board[i][2] == ' ')
		{
			board[i][2] = '#';
			flag = 0;
			return;
			break;
		}
		if (board[i][1] == board[i][2] && board[i][1] == '*' && board[i][0] == ' ')
		{
			board[i][0] = '#';
			flag = 0;
			return;
			break;
		}
	}
	//判断是否列中两子相连
	for (j = 0; j < row; j++)
	{
		if (board[0][j] == board[1][j] && board[0][j] == '*' && board[2][j] == ' ')
		{
			board[2][j] = '#';
			flag = 0;
			return;
			break;
		}
		if (board[1][j] == board[2][j] && board[1][j] == '*' && board[0][j] == ' ')
		{
			board[0][j] = '#';
			flag = 0;
			return;
			break;
		}
	}
	//判断是否对角线中两子相连
	if (board[0][0] == board[1][1] && board[0][0] == '*' && board[2][2] == ' ')
	{
		board[2][2] = '#';
		flag = 0;
		return;
	}
	if (board[0][2] == board[1][1] && board[0][2] == '*' && board[2][0] == ' ')
	{
		board[2][0] = '#';
		flag = 0;
		return;
	}
	if (board[2][0] == board[1][1] && board[2][0] == '*' && board[0][2] == ' ')
	{
		board[0][2] = '#';
		flag = 0;
		return;
	}
	if (board[2][2] == board[1][1] && board[2][2] == '*' && board[0][0] == ' ')
	{
		board[0][0] = '#';
		flag = 0;
		return;
	}
	//只剩中间子
	if (board[0][0] == board[2][2] && board[0][0] == '*' && board[1][1] == ' ')
	{
		board[1][1] = '#';
		flag = 0;
		return;
	}
	else if (board[1][2] == board[2][1] && board[0][0] == '*' && board[1][1] == ' ')
	{
		board[1][1] = '#';
		flag = 0;
		return;
	}
	for (i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][2] && board[i][0] == '*' && board[i][1] == ' ')
		{
			board[i][1] = '#';
			flag = 0;
			return;
			break;
		}
	}
	for (j = 0; j < row; j++)
	{
		if (board[0][j] == board[2][j] && board[0][j] == '*' && board[1][j] == ' ')
		{
			board[1][j] = '#';
			flag = 0;
			return;
			break;
		}
	}
	//玩家第一步下了中间
	if (board[0][0] == board[0][1] && board[0][0] == board[0][2] && board[0][0] == ' ')
	{
		if (board[1][0] == board[1][2] && board[1][0] == ' ' && board[1][1] == '*')
		{
			if (board[2][0] == board[2][1] && board[2][0] == board[2][2] && board[2][0] == ' ')
			{
				board[0][0] = '#';
				return;
			}
		}
	}
	while (flag)
	{
		int x = rand() % row;
		int y = rand() % col;
		//判断该坐标是否已填子
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}
	

//判断游戏状态
char Is_win(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	//判断三行
	for (i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] != ' ')
		{
			return board[i][0];
		}
	}
	//判断三列
	for (j = 0; j < row; j++)
	{
		if (board[0][j] == board[1][j] && board[0][j] == board[2][j] && board[0][j] != ' ')
		{
			return board[0][j];
		}
	}
	//判断对角线
	if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != ' ')
	{
		return board[0][0];
	}
	if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] != ' ')
	{
		return board[0][2];
	}
	//判断平局
	int flag = is_Full(board, row, col);
	if (flag == 1)
	{
		return 'Q';
	}
	//游戏继续
	return 'A';
}